"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _jsxRuntime = require("react/jsx-runtime");

var _antdV = require("antd-v4");

var _react = _interopRequireDefault(require("react"));

/**
 * 数字组件
 *
 * @param FieldDigitProps {
 *     text: number;
 *     moneySymbol?: string; }
 */
var FieldDigit = function FieldDigit(_ref, ref) {
  var text = _ref.text,
      type = _ref.mode,
      render = _ref.render,
      placeholder = _ref.placeholder,
      renderFormItem = _ref.renderFormItem,
      fieldProps = _ref.fieldProps;

  if (type === 'read') {
    var _fieldProps$formatter;

    var fractionDigits = {};

    if (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.precision) {
      fractionDigits = {
        minimumFractionDigits: Number(fieldProps.precision),
        maximumFractionDigits: Number(fieldProps.precision)
      };
    }

    var digit = new Intl.NumberFormat(undefined, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, fractionDigits), (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.intlProps) || {})).format(Number(text));
    var dom = (0, _jsxRuntime.jsx)("span", {
      ref: ref,
      children: (fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$formatter = fieldProps.formatter) === null || _fieldProps$formatter === void 0 ? void 0 : _fieldProps$formatter.call(fieldProps, digit)) || digit
    });

    if (render) {
      return render(text, (0, _objectSpread2.default)({
        mode: type
      }, fieldProps), dom);
    }

    return dom;
  }

  if (type === 'edit' || type === 'update') {
    var _dom = (0, _jsxRuntime.jsx)(_antdV.InputNumber, (0, _objectSpread2.default)({
      ref: ref,
      min: 0,
      placeholder: placeholder
    }, fieldProps));

    if (renderFormItem) {
      return renderFormItem(text, (0, _objectSpread2.default)({
        mode: type
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

var _default = /*#__PURE__*/_react.default.forwardRef(FieldDigit);

exports.default = _default;